package com.ruoyi.web.controller.front;

import com.ruoyi.api.domain.GuidRegionEntity;
import com.ruoyi.api.domain.UserEntity;
import com.ruoyi.api.repository.GuidRegionRepository;
import com.ruoyi.api.service.GuidRegionService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/system/front/region")
public class RegionCotroller extends BaseController {


    @Autowired
    private GuidRegionService guidRegionService;

    @Autowired
    private MongoTemplate mongoTemplate;
    /**
     * 获取用户列表
     */
    @PreAuthorize("@ss.hasPermi('system:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(GuidRegionEntity user,
                              @RequestParam(value = "pageNum", defaultValue = "0") int pageNum,
                              @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) throws IllegalAccessException {
        startPage();
        // 调用服务层方法，获取分页数据
        Page<GuidRegionEntity> resultPage = guidRegionService.selectList(GuidRegionEntity.class,user,pageNum-1,pageSize,mongoTemplate);
        // 调用服务层方法，获取分页数据

        // 转换分页结果为 TableDataInfo
        return getDataTable(resultPage.getContent(), resultPage.getTotalElements());

    }
    /**
     * 新增
     */
    @PreAuthorize("@ss.hasPermi('system:user:add')")
    @Log(title = "新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GuidRegionEntity user)
    {

        user.setCreateBy(getUsername());
        return AjaxResult.success(guidRegionService.insertUser(user));
    }
    /**
     * 新增
     */
    @PreAuthorize("@ss.hasPermi('system:user:add')")
    @Log(title = "修改", businessType = BusinessType.INSERT)
    @PutMapping
    public AjaxResult update(@RequestBody GuidRegionEntity user)
    {

        return AjaxResult.success(guidRegionService.update(GuidRegionEntity.class,user,mongoTemplate));
    }

    /**
     * 根据用户编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:user:query')")
    @GetMapping(value = { "/", "/{id}" })
    public AjaxResult getInfo(@PathVariable(value = "id", required = false) String id)
    {
        return AjaxResult.success(guidRegionService.getInfoById(GuidRegionEntity.class,id,mongoTemplate));
    }

    /**
     * 删除用户
     */
    @PreAuthorize("@ss.hasPermi('system:user:remove')")
    @Log(title = "用户管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return AjaxResult.success(guidRegionService.deleteByIds(GuidRegionEntity.class,ids,mongoTemplate));
    }
}
